package com.xtpt.disaster.controller;


import com.xtpt.disaster.bean.*;
import com.xtpt.disaster.common.PortalResult;
import com.xtpt.disaster.common.PropertyConfig;
import com.xtpt.disaster.common.consts.StatusConst;
import com.xtpt.disaster.common.consts.TaskTypeEnum;
import com.xtpt.disaster.service.*;
import com.xtpt.disaster.utils.ClassInjected;
import com.xtpt.disaster.utils.ParseFileAbsolutePath;
import com.xtpt.disaster.utils.ServerPathUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author jack_xin
 * @ClassName FileBackupController
 * @Desc 文件备份Controller
 * @date 2021年11月17日 16:05
 * @Version TODO
 */
@RestController
@Api(tags = "文件备份接口", value = "/filebackup")
@CrossOrigin
@RequestMapping("/filebackup")
public class FileBackupController {
    @Autowired
    QuartzEntity quartzEntity;

    @Autowired
    ServerPathUtil serverPathUtil;

    @Autowired
    PropertyConfig config;

    @Autowired
    DBNodeInfoService dbNodeInfoService;

    @Autowired
    NodeInfoManageService nodeInfoManageService;

    @Autowired
    ParseFileAbsolutePath parseFileAbsolutePath;

    @Autowired
    FileBackupService fileBackupService;

    @Autowired
    JobDetailService jobDetailService;

    @Autowired
    TaskInfoManageService taskInfoManageService;

    @ApiOperation(tags = "文件备份方法，接收数据封装类taskInfo，根据jobId得到所需信息完成作业的写入", value = "/filetrans", httpMethod = "POST", produces = "application/json")
    @PostMapping("/backup")
    public PortalResult FileBackup(@RequestBody TaskInfo taskInfo) {
        Long clientId = taskInfo.getNodeId();
        NodeInfo client = nodeInfoManageService.selectById(clientId);

        TaskTypeEnum taskType = TaskTypeEnum.getObject(taskInfo.getTaskType());
        String taskClientPath = taskInfo.getTaskClientPath();
        Integer dataType = taskInfo.getTaskFileType();


        String fileServerPath = serverPathUtil.getServerPath(taskInfo.getTaskId().toString(), null, dataType, null, null);

        JobDetail jobDetail = ClassInjected.injectJobDetail(taskInfo, fileServerPath);

        //String serverPath = "\\home\\jack_xin\\test";
        PortalResult ans = fileBackupService.FileBackup(client, taskType.getValue(), taskClientPath, fileServerPath);

        Date jobEndTime = new Date();
        jobDetail.setJobEndTime(jobEndTime);

        if (ans.isOK()) {
            jobDetail.setJobStatus(StatusConst.SUCCESS);
        }
        else {
            jobDetail.setJobStatus(StatusConst.ERROR);
        }
        String command = (String) ans.getData();
        String result = ans.getMsg();

        jobDetail.setJobCommand(command);
        jobDetail.setJobResult(result);

        jobDetailService.insertSelective(jobDetail);
        return PortalResult.ok();
    }
}
